<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>子系统2</title>
    <!--    <script type="text/javascript">-->
    <!--        document.write("<script type='text/javascript' src='/static/js/jquery-3.1.1.min.js?random=" + Math.random() + "'></s" + "cript>");-->
    <!--    </script>-->
</head>
<body>
<h1>欢迎来到子系统2！！</h1>
<p id="msg"></p>

</body>
</html>

<script type="text/javascript" src="/static/js/jquery-3.1.1.min.js"></script>
<script type="text/javascript">

    window.onload = function () {

        $(document).ready(function () {



            $.ajax({
                type: 'GET', // 请求的方式
                url: '/user/info',  // 请求的 URL 地址
                headers: {"Authorization": "Bearer " + localStorage.getItem("token")},
                success: function (res, textStatus, xhr) { // 请求成功之后的回调函数

                    // debugger
                    if (res.code == 200) {

                        $("#msg").text("用户：" + res.data.userName + " 已登录")
                    }

                    /**
                     * 后面两个是统一处理：前端的请求拦截器里面处理
                     *
                     */
                    // debugger
                    if (res.code == 401) {

                        //需重新登录
                        location.href = res.data;
                    }

                    // debugger
                    var refreshToken = xhr.getResponseHeader('refresh-token')
                    if (refreshToken) {

                        //替换token
                        alert(refreshToken);
                        localStorage.setItem("token", refreshToken);
                    }

                }
            })

        })


    }


    function parseUrlTokenToLocalStore() {
        return new Promise((resolve, reject) => {

            var token = getParam("token");
            if (token == undefined || token == "") {
                reject()
                return
            }

            // debugger
            localStorage.setItem("token", token);
            window.location.replace(urlDelParams('token'));
            resolve();

            // setTimeout(function (){
            //     debugger
            //     window.location.replace(urlDelParams('token'))
            //     // window.location = urlDelParams('token');
            //     debugger
            //     resolve()
            // }, 2000)

        })

    }

    /**
     * 获取指定的URL参数值
     * URL:http://www.xxx.com/index?name=123
     * 参数：param URL参数
     * 调用方法:getParam("name")
     * 返回值:123
     * alert(getParam('date'));
     */
    function getParam(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        //search,查询？后面的参数，并匹配正则
        var r = location.search.substr(1).match(reg);
        if (r != null) return decodeURI(decodeURI(r[2]));
    }


    /**
     * 删除url上指定参数，并发起请求
     * window.location.replace(urlDelParams('params'))
     * @param name
     * @return {string}
     */
    function urlDelParams(name) {
        var loca = window.location
        var baseUrl = loca.origin + loca.pathname + '?'
        var query = loca.search.substr(1)
        if (query.indexOf(name) > -1) {
            var obj = {}
            var arr = query.split('&')
            for (var i = 0; i < arr.length; i++) {
                arr[i] = arr[i].split('=')
                obj[arr[i][0]] = arr[i][1]
            }
            delete obj[name]
            var url =
                baseUrl +
                JSON.stringify(obj)
                    .replace(/[\"\{\}]/g, '')
                    .replace(/\:/g, '=')
                    .replace(/\,/g, '&')
            return url
        }
    }

</script>